using System.Collections;
using System.Collections.Generic;
using Socium.Domain.Collections;

namespace XmlConverter {
	public class Registry<TKey, TValue> : IEnumerable<TValue> where TValue : class {
		readonly ICollection<TValue> _values = new LinkedList<TValue>();
		readonly IDictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();

		public Registry(IEnumerable<KeyValuePair<TKey, TValue>> pairs) {
			pairs.Iterate(Add);
		}

		void Add(KeyValuePair<TKey, TValue> pair) {
			_values.Add(pair.Value);
			_dictionary.Add(pair);
		}

		public TValue this[TKey key] {
			get {
				TValue value;
				return _dictionary.TryGetValue(key, out value) ? value : null;
			}
		}

		public IEnumerator<TValue> GetEnumerator() {
			return _values.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}
	}
}